// pages/gift/gift.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list_type: 2,
    page: 1,
    limit: 20,
    total_page: 0,
    BlankCol:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.list_type){
      // this.data.list_type
      this.setData({
        list_type: options.list_type
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page: 1
    })
    this.getList();
  },

  onHide: function () {

  },

  // 订单分类
  changeType: function (e) {
    var types = e.currentTarget.dataset.type;
    this.setData({
      page: 1,
      list_type: types,
      BlankCol:false
    })
    wx.showLoading({
      title: '加载中',
    })
    this.getList();
  },

  getList:function(){
    var params = new Object()
    var that = this;
    params.page = this.data.page;
    params.limit = this.data.limit;
    params.type = this.data.list_type;

    api.POST({
      params: params,
      url: app.globalData.base_url + 'begwine/getList',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var totalPage = paging.total_page;
          var orderList = res.data.data.list;

          if (that.data.page == 1) {
            var itemData = orderList;
          } else {
            var itemData = that.data.product.concat(orderList)
          }

          that.setData({
            total_page: totalPage,
            product: itemData,
            BlankCol:true,
            getCount: res.data.data.report.getCount,
            sendCount: res.data.data.report.sendCount
          })
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },


  //加载更多
  loadMore: function () {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      page: this.data.page + 1
    });
    this.getList();
  },

  //刷新页面
  resetData: function () {
    var that = this
    that.setData({
      page: 1,
      hasMore: true
    });
     this.getList();
  },
  //页面下拉刷新
  onPullDownRefresh: function () {
    var that = this;

    setTimeout(function () {
      that.resetData()
    }, 1000)
    wx.stopPullDownRefresh();
  },

  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },

  //修改讨酒申请状态
  stateModify:function(e){
    var that = this;
    var state = e.currentTarget.dataset.status;
    var id = e.currentTarget.dataset.id;

    if (state==1){
      var text = '是否确认"删除"？'
    } else if (state == 2){
      var text = '是否确认"同意"？'
    } else if (state == 3) {
      var text = '是否确认"不同意"？'
    } else if (state == 4) {
      var text = '是否确认"撤销讨酒"？'
    }

    wx.showModal({
      title: '提示',
      content: text,
      success: function (res) {
        if (res.confirm) {
          
          wx.showLoading({
            title: '加载中',
          })

          api.POST({
            params: { id: id, status: state },
            url: app.globalData.base_url + 'begwine/changeStatus',
            success: function (res) {
              util.hiddenLoading();
              if (util.isEmpty(res.data.code)) {
                that.onShow();
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {

              util.hiddenLoading();
              util.showError("请检查网络");
            }
          })
        }
      }
    })


  },
  //跳转到好友列表
  friendListLink:function(){
    wx.navigateTo({
      url: '/play/pages/beg/beg'
    })
  },
    //跳转到打包送礼
  sendCard:function(){
    wx.navigateTo({
      url: '/play/pages/gift/gift'
    })
  }

})